package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.controller.dto.LoginDto;
import com.example.springboot.controller.request.LoginRequest;
import com.example.springboot.entity.User;
import com.example.springboot.controller.request.UserPageRequset;
import com.example.springboot.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin//解决跨域问题
@RestController//表明将此类注入spring容器，为@Autowired使用
@RequestMapping("/user")
public class UserController {
    @Autowired
    IUserService userService;

//    获取所有用户信息
    @GetMapping("/list")
    public Result list(){
        List<User> users = userService.list();
        return Result.success(users);
    }

//    获取分页查询
    @GetMapping("/page")
    public Result page(UserPageRequset userPageRequset){
        return Result.success(userService.page(userPageRequset));
    }

//    插入 新用户信息
    @PostMapping("/save")
    public Result save(@RequestBody User user){
        userService.save(user);
        return Result.success();
    }

    //通过id实现 查询 用户功能
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        User user = userService.getById(id);
        return Result.success(user);
    }

    //通过id实现 编辑 用户功能
    @PutMapping("/update")
    public Result updateById(@RequestBody User user){
        userService.updateById(user);
        return Result.success();
    }

    //通过id实现 删除 用户功能
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id){
        userService.deleteById(id);
        return Result.success();
    }

    //账户充值接口
    @PostMapping("/account")
    public Result account(@RequestBody User user) {
        userService.handleAccount(user);
        return Result.success();
    }

    //用户登录
    @PostMapping("/login")
    public Result login(@RequestBody LoginRequest request) {
        LoginDto login = userService.login(request);
        return Result.success(login);
    }
}
